<template>
  <div class="model-detail">
    <!-- 头部信息 -->
    <div class="header-section">
      <div class="model-info">
        <div class="logo-section">
          <el-image :src="model.logo" fit="cover" class="model-logo" :preview-src-list="[model.logo]" />
        </div>
        <div class="basic-info">
          <h1 class="model-name">{{ model.name }}</h1>
          <div class="rating-section">
            <el-rate v-model="model.rating" disabled show-score text-color="#ff9900" score-template="{value} 分" />
          </div>
          <div class="tags">
            <el-tag v-for="tag in parsedTags" :key="tag" type="primary" size="small" class="tag-item">
              {{ tag }}
            </el-tag>
          </div>
          <div class="category-cost">
            <el-text class="category">{{ model.category }}</el-text>
            <el-divider direction="vertical" />
            <el-text class="cost">人均 ¥{{ model.averageCost }}</el-text>
          </div>
        </div>
      </div>
    </div>

    <!-- 图片展示 -->
    <div class="images-section" v-if="parsedImages.length > 0">
      <h3>餐厅环境</h3>
      <div class="image-gallery">
        <el-image v-for="(image, index) in parsedImages" :key="index" :src="image" fit="cover" class="gallery-image" :preview-src-list="parsedImages" :initial-index="index" />
      </div>
    </div>

    <!-- 详细信息 -->
    <div class="details-section">
      <el-row :gutter="20">
        <el-col :xs="24" :md="12">
          <el-card class="info-card">
            <template #header>
              <div class="card-header">
                <el-icon><Clock /></el-icon>
                <span>营业信息</span>
              </div>
            </template>
            <div class="info-item">
              <span class="label">营业时间：</span>
              <span class="value">{{ model.businessHours }}</span>
            </div>
            <div class="info-item">
              <span class="label">人均消费：</span>
              <span class="value">¥{{ model.averageCost }}</span>
            </div>
            <div class="info-item">
              <span class="label">注册时间：</span>
              <span class="value">{{ formatDate(model.registerTime) }}</span>
            </div>
            <div class="info-item">
              <span class="label">营业状态：</span>
              <el-tag :type="model.isEnabled ? 'success' : 'danger'" size="small">
                {{ model.isEnabled ? '营业中' : '暂停营业' }}
              </el-tag>
            </div>
          </el-card>
        </el-col>

        <el-col :xs="24" :md="12">
          <el-card class="info-card">
            <template #header>
              <div class="card-header">
                <el-icon><Location /></el-icon>
                <span>位置信息</span>
              </div>
            </template>
            <div class="info-item">
              <span class="label">省份：</span>
              <span class="value">{{ model.province }}</span>
            </div>
            <div class="info-item">
              <span class="label">城市：</span>
              <span class="value">{{ model.city }}</span>
            </div>
            <div class="info-item">
              <span class="label">详细地址：</span>
              <span class="value">{{ model.address }}</span>
            </div>
            <div class="info-item">
              <span class="label">坐标：</span>
              <span class="value">{{ model.position }}</span>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 联系信息 -->
    <div class="contact-section">
      <el-card class="contact-card">
        <template #header>
          <div class="card-header">
            <el-icon><Phone /></el-icon>
            <span>联系方式</span>
          </div>
        </template>
        <el-row :gutter="20">
          <el-col :xs="24" :md="8" v-for="item in phoneNumbers">
            <div class="contact-item" v-if="item.name">
              <span class="label">联系人：</span>
              <span class="value">{{ item.name }}</span>
            </div>
            <div class="contact-item">
              <span class="label">电话：</span>
              <el-link :href="`tel:${item.phone}`" type="primary">
                {{ item.phone }}
              </el-link>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>

    <!-- 描述信息 -->
    <div class="description-section" v-if="model.description">
      <el-card>
        <template #header>
          <div class="card-header">
            <el-icon><Document /></el-icon>
            <span>餐厅介绍</span>
          </div>
        </template>
        <p class="description-text">{{ model.description }}</p>
      </el-card>
    </div>

    <!-- 操作按钮 -->
    <div class="actions-section">
      <el-row :gutter="16">
        <el-col :xs="24" :sm="8">
          <el-button type="primary" size="large" block @click="handleCall">
            <el-icon><Phone /></el-icon>
            立即致电
          </el-button>
        </el-col>
        <el-col :xs="24" :sm="8">
          <el-button type="success" size="large" block @click="handleNavigate">
            <el-icon><Location /></el-icon>
            导航到店
          </el-button>
        </el-col>
        <el-col :xs="24" :sm="8">
          <el-button :type="isFavorited ? 'warning' : 'default'" size="large" block @click="toggleFavorite">
            <el-icon><Star /></el-icon>
            {{ isFavorited ? '已收藏' : '收藏' }}
          </el-button>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Clock, Location, Phone, Document, Star } from '@element-plus/icons-vue'

import http from '@/apis/shop'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()

// 餐厅数据
const model = ref({
  name: '东川攀枝花米线',
  code: 'DCKYW001',
  contactPerson: '张伟',
  phone: '13800001111',
  email: 'zhangwei@bjkyw.com',
  logo: 'http://8.138.239.201:9103/upload/2025/09/01/715004156764229.jpg',
  province: '云南省',
  city: '昆明市',
  address: '东川区福盛巷',
  businessLicenseNo: '91110105MA009XW123',
  registerTime: '2025-08-21 14:17:22',
  isEnabled: true,
  rating: 4.8,
  businessHours: '10:00 - 22:00',
  averageCost: 120,
  tagsJson: '["中餐","老字号","推荐"]',
  category: '餐饮',
  position: '39.906712,116.397481',
  imageUrls: '["http://8.138.239.201:9103/upload/2025/09/01/715007362969669.jpg"]',
  description: '地道风味',
  htmlContent: null,
  createTime: '2025-08-21 14:17:22',
  updateTime: '2025-09-01 20:19:20',
  createUserId: '1300000000111',
  createUserName: 'admin',
  updateUserId: '1300000000111',
  updateUserName: '系统管理员',
  id: '711032176341061'
})
const phoneNumbers = ref<Array<any>>([])
// 收藏状态
const isFavorited = ref(false)

// 解析标签
const parsedTags = computed(() => {
  try {
    return JSON.parse(model.value.tagsJson)
  } catch {
    return []
  }
})

// 解析图片
const parsedImages = computed(() => {
  try {
    return JSON.parse(model.value.imageUrls)
  } catch {
    return []
  }
})

// 格式化日期
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleDateString('zh-CN')
}

// 拨打电话
const handleCall = () => {
  window.open(`tel:${model.value.phone}`)
}

// 导航到店
const handleNavigate = () => {
  if (!model.value.position) {
    ElMessage.info('位置未录入')
    return
  }
  const [lng, lat] = JSON.parse(model.value.position)
  const url = `https://uri.amap.com/marker?position=${lng},${lat}&name=${model.value.name}&src=kmdc`
  window.open(url)
}

// 切换收藏状态
const toggleFavorite = () => {
  isFavorited.value = !isFavorited.value
  ElMessage.success(isFavorited.value ? '收藏成功' : '取消收藏')
}

onMounted(() => {
  if (route.query.id) {
    http.fetchById(route.query.id as string).then((response) => {
      let { code, message, result } = response.data
      if (code !== 200) {
        ElMessage.error(message)
        return
      }
      model.value = result
      if (result.phoneNumbers) {
        phoneNumbers.value = JSON.parse(result.phoneNumbers)
      }
    })
  }
})
</script>

<style scoped>
.model-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header-section {
  margin-bottom: 30px;
}

.model-info {
  display: flex;
  gap: 20px;
  align-items: flex-start;
}

.logo-section {
  flex-shrink: 0;
}

.model-logo {
  width: 120px;
  height: 120px;
  border-radius: 12px;
  border: 2px solid #f0f0f0;
}

.basic-info {
  flex: 1;
}

.model-name {
  font-size: 28px;
  font-weight: bold;
  margin: 0 0 15px 0;
  color: #333;
}

.rating-section {
  margin-bottom: 15px;
}

.tags {
  margin-bottom: 15px;
}

.tag-item {
  margin-right: 8px;
  margin-bottom: 8px;
}

.category-cost {
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #666;
}

.images-section {
  margin-bottom: 30px;
}

.images-section h3 {
  margin-bottom: 15px;
  font-size: 20px;
  color: #333;
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 15px;
}

.gallery-image {
  width: 100%;
  height: 150px;
  border-radius: 8px;
}

.details-section {
  margin-bottom: 30px;
}

.info-card,
.contact-card {
  height: 100%;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
}

.info-item,
.contact-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding: 8px 0;
  border-bottom: 1px solid #f5f5f5;
}

.info-item:last-child,
.contact-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.label {
  font-weight: 500;
  color: #666;
  min-width: 80px;
  margin-right: 10px;
}

.value {
  color: #333;
  flex: 1;
}

.contact-section {
  margin-bottom: 30px;
}

.description-section {
  margin-bottom: 30px;
}

.description-text {
  font-size: 16px;
  line-height: 1.6;
  color: #666;
  margin: 0;
}

.actions-section {
  margin-top: 30px;
}

@media (max-width: 768px) {
  .model-detail {
    padding: 15px;
  }

  .model-info {
    flex-direction: column;
    text-align: center;
  }

  .model-logo {
    width: 100px;
    height: 100px;
    margin: 0 auto;
  }

  .model-name {
    font-size: 24px;
  }

  .image-gallery {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .info-item,
  .contact-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }

  .label {
    min-width: auto;
    margin-right: 0;
  }
}
</style>
